﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace GWEB.WebFrame.Activitys
{
    public class ActivitySpController : PageInfoBase
    {
        [ActionFilter(IsLogin = false)]
        public ActionResult Index(String Id, String KeyCode)
        {
            ViewBag.Id = Id;
            ViewBag.KeyCode = KeyCode;
            return View(BasePage.V("Activitys/ActivitySp"));
        }
        [ActionFilter(IsLogin = false)]
        [HttpPost]
        public JsonResult ActivityInfo(String id, String keyCode)//活动信息
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Code("0001", "您没有权限"));
            String sql = "";
            if (!string.IsNullOrEmpty(id))
            {
                sql = @"SELECT * FROM cu_Activity where id= '" + id + "'";
            }
            else
            {
                return BasePage.Json(ResultData.Code("0009", "参数为空"));
            }

            BasePage.bd.ColumnNameToLower = false;
            DataTable dt = BasePage.bd.ExecuteTable(sql);
            String str = JsonConvert.SerializeObject(dt);
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));
        }

        [ActionFilter(IsLogin = false)]
        [HttpPost]
        public JsonResult StudentInfo(String Aid, String siginState, String phoneNumber, String id, String keyCode)//签到学生信息
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Code("0001", "您没有权限"));
            String sql = @"SELECT * FROM cu_Activity_SignUp where 1=1";
            if (!string.IsNullOrEmpty(Aid))
            {
                sql += "and ActivityId= '" + Aid + "'";
            }
            else
            {
                return BasePage.Json(ResultData.Code("0009", "参数为空"));
            }


            if (!string.IsNullOrEmpty(siginState))
            {
                if (Convert.ToInt32(siginState) == 3)
                {
                    sql += "and (state= 3 or state= 5)";
                }
                else if (Convert.ToInt32(siginState) == 4)
                {
                    sql += "and state= '" + siginState + "'";
                }
            }


            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += "and Tel= '" + phoneNumber + "'";
            }

            BasePage.bd.ColumnNameToLower = false;
            DataTable dt = BasePage.bd.ExecuteTable(sql);
            String str = JsonConvert.SerializeObject(dt);
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));
        }
        [ActionFilter(IsLogin = false)]
        [HttpPost]
        public JsonResult KHInfo(String Tel, String Aid, String id, String keyCode)//客户信息
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Code("0001", "您没有权限"));
            String sql = "";
            String sql1 = "";
            if (!string.IsNullOrEmpty(Aid) || !string.IsNullOrEmpty(Tel))
            {
                sql = @"SELECT *, CASE isnull((select count(*) from cu_Activity_SignUp where cu_Activity_SignUp.Tel = cu_Customers.CustTel and cu_Activity_SignUp.ActivityId=" + Aid;
                sql1 = ") ,0) when '0' then 0 ELSE '1' END as isBM,CASE isnull((select count(*) from cu_Contract where cu_Contract.Cid = cu_Customers.id  and (isnull(SurplusNumber,0)-isnull(GiveCourseNumber, 0) > 0 and isnull(CourseEnd,'2099-12-31')> GETDATE()) and status in (1, 2, 3, 4) ) ,0) when '0' then 0 ELSE '1' END as isHY FROM cu_Customers where CustState = 6 ";
            }
            else
            {
                return BasePage.Json(ResultData.Code("0009", "参数为空"));
            }

            if (!string.IsNullOrEmpty(Tel))
            {
                sql1 += "and CustTel= '" + Tel + "'";
            }
            String sql2 = sql + sql1;
            BasePage.bd.ColumnNameToLower = false;
            DataTable dt = BasePage.bd.ExecuteTable(sql2);
            String str = JsonConvert.SerializeObject(dt);
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));
        }



        [ActionFilter(IsLogin = false)]
        [HttpPost]
        public JsonResult ActivityBM(String v, String id, String keyCode)//活动报名
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Code("0001", "您没有权限"));
            var Activity = JsonConvert.DeserializeObject<Activity_SignUp>(v);
            String sql = @"SELECT * FROM cu_Activity_SignUp where Cid= '" + Activity.Cid + "' and ActivityId= '" + Activity.ActivityId + "'";
            BasePage.bd.ColumnNameToLower = false;
            DataTable dt = BasePage.bd.ExecuteTable(sql);
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    return BasePage.Json(ResultData.Code("0009", "已存在"));
                }
                else
                {
                    String sqls = @"INSERT INTO cu_Activity_SignUp (Cid,ActivityId,Name,Tel,LinkName,IsMember,DecClass,Decprice,Soure,createid,createdate,LeagueId,guid,state)
                     VALUES ('" + Activity.Cid + "','" + Activity.ActivityId + "','" + Activity.Name + "','" + Activity.Tel + "','" + Activity.LinkName + "','" + Activity.IsMember + "','" + Activity.DecClass + "','" + Activity.Decprice + "','" + Activity.Soure + "','" + BasePage.GetUserId(Session) + "','" + DateTime.Now + "','1','" + Guid.NewGuid().ToString("N") + "','" + Activity.state + "')";
                    BasePage.bd.ColumnNameToLower = false;
                    dt = BasePage.bd.ExecuteTable(sqls);
                }
            }

            BasePage.bd.ColumnNameToLower = false;
            String str = JsonConvert.SerializeObject(dt);
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));
        }

        [ActionFilter(IsLogin = false)]
        [HttpPost]
        public JsonResult CancelBM(String Cid, String id, String keyCode)//取消报名
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Code("0001", "您没有权限"));
            String sqls = "";
            if (!string.IsNullOrEmpty(Cid))
            {
                sqls = @"delete from  cu_Activity_SignUp  where Cid=" + Cid;
            }
            else
            {
                return BasePage.Json(ResultData.Code("0009", "参数为空"));
            }
            //if (!Fun.IDVerify(id, keyCode))
            //    return BasePage.Json(ResultData.Code("0001", "您没有权限"));    

            BasePage.bd.ColumnNameToLower = false;
            DataTable dt = BasePage.bd.ExecuteTable(sqls);
            String str = JsonConvert.SerializeObject(dt);
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));
        }


        [ActionFilter(IsLogin = false)]
        [HttpPost]
        public JsonResult signIn(String id, String type, String vid, String keyCode)//签到，取消签到
        {
            if (!Fun.IDVerify(vid, keyCode))
                return BasePage.Json(ResultData.Code("0001", "您没有权限"));
            String sqls = "";
            if (!string.IsNullOrEmpty(id) || !string.IsNullOrEmpty(type))
            {
                sqls = @"UPDATE  cu_Activity_SignUp  SET state = '" + type + "',subscribeDate = '" + DateTime.Now + "' where id=" + id;
            }
            else
            {
                return BasePage.Json(ResultData.Code("0009", "参数为空"));
            }
            BasePage.bd.ColumnNameToLower = false;
            DataTable dt = BasePage.bd.ExecuteTable(sqls);
            String str = JsonConvert.SerializeObject(dt);
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));
        }
        [ActionFilter(IsLogin = false)]
        [HttpPost]
        public JsonResult UpSigin(String type, String Upid, String id, String keyCode)//签到0，取消签到1
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Code("0001", "您没有权限"));
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            List<int> objs = Serializer.Deserialize<List<int>>(Upid);
            BasePage.bd.ColumnNameToLower = false;
            DataTable dt = new DataTable();
            String sqls = "";
            if (!string.IsNullOrEmpty(type) || objs.Count > 0)
            {
                if (Convert.ToInt32(type) == 0)
                {
                    for (var i = 0; i < objs.Count; i++)
                    {
                        sqls = @"UPDATE  cu_Activity_SignUp  SET state = '4',subscribeDate = '" + DateTime.Now + "' where id=" + objs[i];
                        dt = BasePage.bd.ExecuteTable(sqls);
                    }
                }
                else if (Convert.ToInt32(type) == 1)
                {
                    for (var i = 0; i < objs.Count; i++)
                    {
                        sqls = @"UPDATE  cu_Activity_SignUp  SET state = '5' where id=" + objs[i];
                        dt = BasePage.bd.ExecuteTable(sqls);
                    }
                }
            }
            else
            {
                return BasePage.Json(ResultData.Code("0009", "参数为空"));
            }
            //if (!Fun.IDVerify(id, keyCode))
            //    return BasePage.Json(ResultData.Code("0001", "您没有权限"));    
            String str = JsonConvert.SerializeObject(dt);
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));
        }
        protected override void FieldAddInit(DataRow data)
        {
            base.FieldAddInit(data);
        }
        protected override void FieldEditInit(DataRow data)
        {
            ViewBag.Lx = 1;
            base.FieldEditInit(data);
        }
    }
}

public class Activity_SignUp
{
    public int? id;
    public int? Cid;
    public int? ActivityId;
    public string Name;
    public string Tel;
    public string LinkName;
    public int? IsMember;
    public string Remark;
    public int? state;
    public int? DecClass;
    public decimal Decprice;
    public string Soure;
    public int? createid;
    public DateTime? subscribeDate;
    public DateTime? createdate;
    public int? LeagueId;
    //public string guid;
}
/// <summary>    
/// 实体转换辅助类    
/// </summary>    
public class ModelConvertHelper<T> where T : new()
{
    public static List<T> ConvertToModel(DataTable dt)
    {
        // 定义集合    
        List<T> ts = new List<T>();

        // 获得此模型的类型   
        Type type = typeof(T);
        string tempName = "";

        foreach (DataRow dr in dt.Rows)
        {
            T t = new T();
            // 获得此模型的公共属性      
            PropertyInfo[] propertys = t.GetType().GetProperties();
            foreach (PropertyInfo pi in propertys)
            {
                tempName = pi.Name;  // 检查DataTable是否包含此列    

                if (dt.Columns.Contains(tempName))
                {
                    // 判断此属性是否有Setter      
                    if (!pi.CanWrite) continue;

                    object value = dr[tempName];
                    if (value != DBNull.Value)
                        pi.SetValue(t, value, null);
                }
            }
            ts.Add(t);
        }
        return ts;
    }
}
